package cn.wolfcode.trip.app.controller;


import cn.wolfcode.trip.app.util.FreemarkerUtil;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.RegionStrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.Writer;
import java.util.HashMap;

@RestController
@RequestMapping("/regions")
public class RegionController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ServletContext ctx;

    @GetMapping
    public Object getRegions() {
        return regionService.listAll();
    }


    @GetMapping("/hots")
    public Object getHotsByState(Integer state) {
        return regionService.listHotsByState(state);
    }


    @GetMapping("/{id}/strategies")
    public void getHotsStrategies(RegionStrategyQueryObject qo, HttpServletResponse response) throws Exception{
        //地区热推荐攻略，不分页
        qo.setState(Strategy.STATE_COMMEND);
        qo.setPageSize(0);
        PageInfo<Strategy> commend = strategyService.queryForList(qo);

        //地区全部攻略，分页
        qo.setState(-1);
        qo.setPageSize(2);
        PageInfo<Strategy> list = strategyService.queryForList(qo);

        //将对象封装返回
        HashMap<String, Object> map = new HashMap<>();
        //将地区的id封装返回
        map.put("id", qo.getRegionId());
        map.put("commend", commend);
        map.put("list", list);

        //设置返回的类型
        response.setContentType("text/html;charset=UTF-8");
        //获取地址
        String dir = ctx.getRealPath("template");
        //输出流
        FreemarkerUtil.wirteTempDate(dir, "strategyContent.ftl", map, response.getWriter());

    }

}
